import { request, useUserStore, clearEmpty } from "jmash-core";
import type { AxiosPromise } from "axios";
import type {
  JobDefineApi,
  JobDefineReq,
  JobDefineModel,
  JobDefinePage,
  JobDefineList,
  JobDefineKey,
  JobDefineKeyList,
  JobDefineEnableKey,
  JobDefineCreateReq,
  JobDefineUpdateReq,
  JobDefineEntry,
} from "./types";

class JobDefineApiImpl implements JobDefineApi {
  getKey(model: JobDefineModel): JobDefineKey {
    return {
      jobName: model.jobName,
    } as JobDefineKey;
  }

  // 查询翻页信息计划任务定义
  findPage(query?: JobDefineReq): AxiosPromise<JobDefinePage> {
    const tenant = useUserStore().user.tenant;
    if (query) {
      query.hasEnable = query.enable != undefined;
    }
    clearEmpty(query);
    return request({
      url: "/v1/jobs/job_define/page/" + tenant,
      method: "get",
      params: query,
    });
  }

  // 查询列表信息计划任务定义
  findList(query?: JobDefineReq): AxiosPromise<JobDefineList> {
    const tenant = useUserStore().user.tenant;
    clearEmpty(query);
    return request({
      url: "/v1/jobs/job_define/list/" + tenant,
      method: "get",
      params: query,
    });
  }

  // 查询计划任务定义
  findById(query: JobDefineKey): AxiosPromise<JobDefineModel> {
    const tenant = useUserStore().user.tenant;
    clearEmpty(query);
    return request({
      url: "/v1/jobs/job_define/id/" + tenant + "/" + query.jobName,
      method: "get",
    });
  }

  // 创建实体计划任务定义
  create(data: JobDefineCreateReq): AxiosPromise<JobDefineModel> {
    data.tenant = useUserStore().user.tenant;
    clearEmpty(data);
    return request({
      url: "/v1/jobs/job_define",
      method: "post",
      data: data,
    });
  }

  // 修改实体计划任务定义
  update(data: JobDefineUpdateReq): AxiosPromise<JobDefineModel> {
    data.tenant = useUserStore().user.tenant;
    clearEmpty(data);
    return request({
      url: "/v1/jobs/job_define",
      method: "patch",
      data: data,
    });
  }

  // 启用/禁用
  enable(key: JobDefineKey, enable: boolean): AxiosPromise<boolean> {
    let data: JobDefineEnableKey = { ...key };
    data.tenant = useUserStore().user.tenant;
    data.enable = enable;
    clearEmpty(data);
    return request({
      url: "/v1/jobs/job_define/enable",
      method: "put",
      data: data,
    });
  }

  // 删除计划任务定义
  delete(query: JobDefineKey): AxiosPromise<JobDefineModel> {
    query.tenant = useUserStore().user.tenant;
    clearEmpty(query);
    return request({
      url: "/v1/jobs/job_define/id",
      method: "delete",
      params: query,
    });
  }

  // 批量删除计划任务定义
  batchDelete(keys: JobDefineKey[]): AxiosPromise<number> {
    const ids = keys.map((item) => item.jobName);
    const data = {
      tenant: useUserStore().user.tenant,
      jobName: ids,
    } as JobDefineKeyList;
    clearEmpty(data);
    return request({
      url: "/v1/jobs/job_define/batch",
      method: "put",
      data: data,
    });
  }
  //获取任务分类接口
  getClassify(): AxiosPromise<JobDefineEntry> {
    const tenant = useUserStore().user.tenant;
    return request({
      url: "/v1/jobs/job_define/classify/" + tenant,
      method: "get",
    });
  }
}
const jobDefineApi = new JobDefineApiImpl();

export { jobDefineApi };
